import model from "../model/model.js";
import KoaRouter from 'koa-router'
import {Op} from "sequelize";

const router = new KoaRouter()

//查询列表
router.get('/', async (ctx) => {
    const info = await model.findAll();
    ctx.body = {
        code: 200,
        msg: 'pass',
        data: info
    }
})

//添加列表
router.post('/addEntry', async (ctx) => {
    const values = ctx.request.body
    console.log(values)
    const info = await model.create(values);
    ctx.body = {
        code: 200,
        msg: 'pass',
        data: info
    }
})

//查询列表
router.get('/getPatch', async (ctx) => {
    const {name} = ctx.query
    const patch = await model.findAll({
        where: {
           'name':{
               [Op.like]: '%' + name + '%'
           }
        }
    })

    ctx.body = {
        code: 200,
        msg: 'pass',
        data: patch
    }
})

router.delete('/delEntry', async (ctx) => {
    const query = ctx.query
    const delInfo = await model.destroy({
        where: {
            id: query.id
        }
    })
    ctx.body = {
        code: 200,
        msg: 'pass',
        data: delInfo
    }
})

router.put('/updateEntry', async (ctx) => {
    const query = ctx.query
    const values = ctx.request.body
    const info = await model.update(values, {
        where: {
            id: query.id
        }
    })
    ctx.body = {
        code: 200,
        msg: 'pass',
        data: info
    }
})

export default router